package com.central.apps.handler;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.AppCommonService;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.common.util.ToolUtil;
import com.central.common.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class MenuCollectHandler extends LvBaseHandler {

    @Autowired
    private HandlerService handlerService;

    @Autowired
    private AppCommonService appCommonService;

    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if("sgdmenucollectionnewApp".equals(app)){
            String menuId = MapUtils.getString(data,"menuid");
            List< Map<String,Object>> list = handlerService.findBySql("select parentid from sys_menu where id="+menuId);
            Map<String,Object> mp = new HashMap<>();
            parentMenuId(menuId,mp);
            if(ToolUtil.isNotEmpty(mp.get("firstmenuid"))){
                data.put("firstmenuid",MapUtils.getString(mp,"firstmenuid"));
            }
        }

    }

    //递归查询上级节点
    public void parentMenuId(String menuId,Map<String,Object> mp){
        List<Map<String,Object>> newList = handlerService.findBySql("select id,parentid from sys_menu where id="+menuId);
        if(CollectionUtils.isNotEmpty(newList)){
            for(Map<String,Object> menuMap : newList){
                    String id = MapUtils.getString(menuMap,"id");
                    String parentId = menuMap.get("parentid")==null?"":MapUtils.getString(menuMap,"parentid");
                    if(ToolUtil.isNotEmpty(parentId)&&!"-1".equals(parentId)&&!"0".equals(parentId)){
                        parentMenuId(parentId,mp);
                    }else{
                        mp.put("firstmenuid",id);
                    }
                }
        }else{
            mp.put("firstmenuid",menuId);
        }

    }


    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        try{
            if(type.equals("save")) {
            if ("sgdmenucollectionnewApp".equals(app)) {
                Integer userId = MapUtils.getInteger(params,"userid");
                List<Map<String,Object>> findList = (List<Map<String, Object>>) MapUtils.getObject(params,"datas");
                if(null != userId){
                    handlerService.updateBySql("delete from gq_sgdmenucollectionnew where userid="+userId);
                    if(ToolUtil.isNotEmpty(findList)){
                        appCommonService.batchSaveOrUpdate("sgdmenucollectionnewApp",findList);
                    }
                }
            }
        }
        }catch (Exception e){
            return Result.failed("保存失败!");
        }

        return Result.succeed("保存成功!");
    }

}
